<?php defined('SYSPATH') or die('No direct script access.');
require_once(APPPATH. 'classes/model/Model_Mysql.php');
require_once(APPPATH. 'classes/model/Model_UserFeeds.php');

class Model_UserComments extends Model_Mysql {
	public function addComment($feedid, $uid, $text) {
		$sql = "INSERT INTO user_comments(feedid,postedby,text,time)
				VALUES($feedid,$uid,:text,".time().")";
		$query = DB::query(Database::INSERT, $sql)->param(':text', $text);
		$result = $this->execute($query);
		
		//increment counter
		$uf = new Model_UserFeeds();
		$uf->onCommentAdded($feedid, $uid);
	}
	public function getFeedComments(&$feeds) {
		$sql = "SELECT * FROM user_comments WHERE feedid in(".implode(",", array_keys($feeds)).")";
		$query = DB::query(Database::SELECT, $sql);
		$result = $this->execute($query);
		foreach ($result as $comment) {
			if(!isset($feeds[$comment['feedid']]['comments'])) {
				$feeds[$comment['feedid']]['comments'] = array();
			}
			$feeds[$comment['feedid']]['comments'][] = $comment;
		}
	}
}